﻿Namespace Utils
    Public NotInheritable Class FileExt
        ''' <summary>
        ''' Take a fileName an return a scramble string.
        ''' </summary>
        ''' <param name="fileName"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function EncodedFileName(ByVal fileName As String, Optional ByVal fileExtention As String = "stbx") As String
            Dim encodedString As String
            Dim cryptoHelper As CryptographyHelper = New CryptographyHelper(CryptographyHelper.EncodingBaseTypes.Hex)
            encodedString = cryptoHelper.GetHash(fileName, CryptographyHelper.HashTypes.MD5)
            Return String.Format("{0}.{1}", encodedString, fileExtention)
        End Function

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="fileName"></param>
        ''' <param name="path"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function EncryptTextFileContent(ByVal fileName As String, Optional ByVal path As String = "") As Boolean
            Dim cryptoHelper As CryptographyHelper = New CryptographyHelper(CryptographyHelper.EncodingBaseTypes.Base64)
            Dim fullFileName As String = System.IO.Path.Combine(path, fileName)
            EncryptTextFileContent = False
            Try
                If System.IO.File.Exists(fullFileName) Then
                    'TestPoint : This is the KEY for encryption
                    Dim encryptedText As String = cryptoHelper.Encrypt(System.IO.File.ReadAllText(fullFileName), "QaZPlM63!")
                    System.IO.File.WriteAllText(fullFileName, encryptedText)
                Else
                    Throw New FileNotFoundException("File Not found in EncryptTextFileContent", fileName)
                End If
                EncryptTextFileContent = True
            Catch ex As Exception
                Throw ex
            End Try
            Return EncryptTextFileContent
        End Function

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="fileName"></param>
        ''' <param name="path"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function DecryptTextFileContent(ByVal fileName As String, Optional ByVal path As String = "") As String
            Dim cryptoHelper As CryptographyHelper = New CryptographyHelper(CryptographyHelper.EncodingBaseTypes.Base64)
            Dim fullFileName As String = System.IO.Path.Combine(path, fileName)
            DecryptTextFileContent = Nothing
            Try
                If System.IO.File.Exists(fullFileName) Then
                    'TestPoint : This is the KEY for encryption
                    DecryptTextFileContent = cryptoHelper.Decrypt(System.IO.File.ReadAllText(fullFileName), "QaZPlM63!")
                Else
                    Throw New FileNotFoundException("File Not found in DecryptTextFileContent", fileName)
                End If
            Catch ex As Exception
                Throw ex
            End Try
            Return DecryptTextFileContent
        End Function
    End Class
End Namespace

